<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="姓名" prop="stuName">
                <el-input
                        v-model="queryParams.name"
                        placeholder="请输入姓名"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="住院号" prop="inpatientNo">
                <el-input
                        v-model="queryParams.inpatientNo"
                        placeholder="请输入住院号"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="状态" prop="settleType">
                <el-select v-model="queryParams.settleType" clearable  class="m-2" placeholder="请选择申请状态">
                    <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="出院日期" prop="effectiveE">
                <el-date-picker
                        v-model="dateRange"
                        value-format="YYYY-MM-DD"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                ></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <!--        <el-row :gutter="10" class="mb8">-->
        <!--            <el-col :span="1.5">-->
        <!--                <el-button-->
        <!--                    type="warning"-->
        <!--                    plain-->
        <!--                    @click="handleGain"-->
        <!--                >获取</el-button>-->
        <!--            </el-col>-->
        <!--            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
        <!--        </el-row>-->

        <el-table v-loading="loading" :data="examinationList" >
            <el-table-column label="姓名" align="center" prop="name"/>
            <el-table-column label="住院号" align="center" prop="inpatientNo"/>
            <el-table-column label="住院次数" align="center" prop="admissTimes"/>
            <el-table-column label="病区名称" align="center" prop="wardName"/>
            <el-table-column label="科室名称" align="center" prop="disDeptName"/>
            <el-table-column label="床号" align="center" prop="bedNo"/>
            <el-table-column label="医保身份" align="center" prop="respName"/>
            <el-table-column label="结算状态" align="center" prop="settleName"/>
          <el-table-column label="出院时间" align="center" prop="effectiveE" >
            <template #default="scope">
              {{ dataFliter(scope.row.effectiveE) }}
            </template>
          </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" @click="handleDetail(scope.row)"
                               v-hasPermi="['audit:audit:detail']">详情
                    </el-button>
                    <el-button link type="primary" v-if="scope.row.jsdPdfId != undefined " @click="handlePdf(scope.row)" v-hasPermi="['audit:audit:pdf']">打印</el-button>
                  <el-button link type="primary" v-if="scope.row.emrWeb != null" @click="handleUrl(scope.row)" v-hasPermi="['audit:audit:url']">病历</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />
      <a ref="target" href="" target="_blank"></a>
        <!--详情信息对话框-->
        <el-dialog title="详情" v-model="detailOpen" width="1000px" append-to-body>
            <el-form :model="patientForm" label-width="100px">
                <el-form-item label="姓名：" prop="name">
                    {{ patientForm.name }}
                </el-form-item>
                <el-form-item label="住院号：" prop="inpatientNo">
                    {{ patientForm.inpatientNo }}
                </el-form-item>
                <el-form-item label="住院次数：" prop="admissTimes">
                    {{ patientForm.admissTimes }}
                </el-form-item>
                <el-form-item label="病区名称：" prop="wardName">
                    {{ patientForm.wardName }}
                </el-form-item>
                <el-form-item label="科室名称：" prop="department">
                    {{ patientForm.disDeptName }}
                </el-form-item>
                <el-form-item label="床号：" prop="bedNo">
                    {{ patientForm.bedNo }}
                </el-form-item>
                <el-form-item label="医保身份：" prop="respName">
                    {{ patientForm.respName }}
                </el-form-item>
                <el-form-item label="结算状态：" prop="settleName">
                    {{ patientForm.settleName }}
                </el-form-item>
                <el-form-item label="出科时间：" prop="effectiveE">
                    <!--            {{patientForm.effectiveE}}-->
                    {{ dataFliter(patientForm.effectiveE) }}
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="detailCancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
        <!--详情信息对话框-->
        <el-dialog title="预览" v-model="pdfOpen" width="1000px" append-to-body>
            <iframe
                width="100%"
                height="700px"
                :src='pdfUrl'
                frameborder="0"
            ></iframe>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="pdfCancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="audit">
import {
    listExaminationAudit,
    delExamination,
    addExamination,
} from "@/api/reimbursement/reimbursement";
import dayjs from "dayjs"
import {getExaminationAudit, getPdfAudit, getPdfFromSqlSeverAudit} from "@/api/audit/audit";
import {getCurrentInstance, onDeactivated, onMounted, reactive, ref} from "vue";

const {proxy} = getCurrentInstance();
const activeName = ref('first')
const examinationList = ref([]);
const open = ref(false);
const detailOpen = ref(false)
const loading = ref(true);
const showSearch = ref(true);
const pdfOpen = ref(false)
const pdfUrl = ref('')
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([formattedDate(Date.now()), formattedDate(Date.now())]);
const statusList = ref([{value:0,label:"0末结算病人"},{value:1,label:"1结算末出纳病人"},{value:2,label:"2已出纳病人"}])
const reimbursementForm = ref([]);
const data = reactive({
    form: {},
    patientForm: {},
    queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: null,
        inpatientNo: null,
        settleType: 0,
        status: null,
    },
    rules: {}
});

const {queryParams, form, rules, patientForm} = toRefs(data);

function stateFilter(state) {
    let txt = ""
    switch (state) {
        case "0":
            txt = "未开始";
            break;
        case "1":
            txt = "申请中";
            break;
        case "2":
            txt = "已完成";
            break;
        case "3":
            txt = "驳回";
            break;
    }
    return txt
}

function genderFilter(state) {
    let txt = ""
    switch (state) {
        case 1:
            txt = "男";
            break;
        case 2:
            txt = "女";
            break;
    }
    return txt
}

function dataFliter(val, format = "YYYY-MM-DD hh:mm:ss") {
    return dayjs(val).format(format);
}
// 详情取消按钮
function pdfCancel() {
    pdfOpen.value = false;
    pdfUrl.value = ''
}
function formattedDate(time) {
    const date = new Date(time);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const tem = year + '-' + month + '-' + day
    return tem;
}

/** 查询报销列表 */
function getList() {
    loading.value = true;
    listExaminationAudit(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
        examinationList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 添加/修改取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 详情取消按钮
function detailCancel() {
    detailOpen.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        name: null
    };
    proxy.resetForm("examinationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd(row) {
    reset();
    form.value = row;
    form.value.settleType = '1'
    form.value.settleName = '1结算末出纳病人'
    addExamination(form.value).then(response => {
        proxy.$modal.msgSuccess("申请成功");
        // open.value = false;
        getList();
    });
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getExaminationAudit(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改报销信息";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["examinationRef"].validate(valid => {
        if (valid) {
            // if (form.value.id != null) {
            //   updateExamination(form.value).then(response => {
            //     proxy.$modal.msgSuccess("修改成功");
            //     open.value = false;
            //     getList();
            //   });
            // } else {
            form.value.parentId = form.value.id
            form.value.id = null
            addExamination(form.value).then(response => {
                proxy.$modal.msgSuccess("新增成功");
                open.value = false;
                getList();
            });
            // }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除驾校-体检信息编号为"' + _ids + '"的数据项？').then(function () {
        return delExamination(_ids);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {
    });
}

/** 详情按钮操作 */
function handleDetail(row) {
    detailOpen.value = true;
    getExaminationAudit(row.id).then(response => {
        patientForm.value = response.data.patient;
    })
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('system/examination/export', {
        ...queryParams.value
    }, `examination_${new Date().getTime()}.xlsx`)
}


function setup() {
    const timer = ref(0)
    const index = ref(0)
    onMounted(() => { //组件挂载时的生命周期执行的方法
        timer.value = window.setInterval(function logname() {
            // 其他定时执行的方法
            index.value = index.value + 1
            getList();
        }, 10000);
    })
    onDeactivated(() => { //离开当前组件的生命周期执行的方法
        window.clearInterval(timer.value);
    })
}


function setupGain() {
    const timer = ref(0)
    const index = ref(0)
    onMounted(() => { //组件挂载时的生命周期执行的方法
        timer.value = window.setInterval(function logname() {
            // 其他定时执行的方法
            index.value = index.value + 1
            handleGain()
        }, 15000);
    })
    onDeactivated(() => { //离开当前组件的生命周期执行的方法
        window.clearInterval(timer.value);
    })
}

/** 打印按钮操作 */
function handlePdf(row) {
    pdfOpen.value = true;
    pdfUrl.value = ''
    if(row.jsdPdfId != null){
        getPdfAudit(row.jsdPdfId).then(response =>{
            pdfUrl.value = 'data:application/pdf;base64,' + response.data.jsdPdf
        }).catch(response => {
            console.log(response)
        })
    } else {
        getPdfFromSqlSeverAudit(row.inpatientNo).then(response => {
            pdfUrl.value = 'data:application/pdf;base64,' + response.data.jsdPdf
        }).catch(response => {
            console.log(response)
        })
    }

}

/** 超链接按钮操作 */
function handleUrl(row) {
  let target = proxy.$refs.target
  target.setAttribute('href', row.emrWeb)
  target.click()
}

// setupGain()
setup()
getList();
</script>
<style type="text/css">
.el-row {
    display: flex;
    flex-wrap: wrap
}

.el-col {
    border-radius: 4px;
    align-items: stretch;
}

.el-card {
    min-width: 100%;
    height: 100%;
    margin-right: 20px;
    /*transition: all .5s;*/
}

.el-card:hover {
    margin-top: -5px;
}
</style>


